package com.ck.ckcloud.networkmanagement.domain.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ck.ckcloud.networkmanagement.core.enums.ManageStatusEnum;
import com.ck.ckcloud.networkmanagement.core.enums.ResourceType;
import com.ck.ckcloud.networkmanagement.domain.entity.Resource;
import com.ck.ckcloud.networkmanagement.infrastructure.persistence.DO.ResourceDO;
import com.ck.ckcloud.networkmanagement.infrastructure.repository.ResourceRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 网管领域服务
 *
 * @author Aiden <a href="mailto:adam602260@gmail.com"/>
 * @version NetworkManagementService.java v0.1
 * @create 2019-09-20 19:11
 */
@Service
public class NetworkManagementService {

	@Autowired
	private ResourceRepository resourceRepository;

	/**
	 * 分页查询资源
	 * @param page
	 * @return
	 */
	public IPage listResourceByPage(Page page){
		IPage iPage = resourceRepository.listByPage(page);
		List<Resource> resourceList = new ArrayList<>();
		iPage.getRecords().stream().forEach(r -> {
			ResourceDO resourceDO = (ResourceDO) r;
			Resource resource = new Resource();
			BeanUtils.copyProperties(resourceDO, resource);
			resource.setResourcetype(ResourceType.getNameById(resourceDO.getResourcetype()));
			resource.setManagestatus(ManageStatusEnum.getManageStatus(resourceDO.getManagestatus()));
			resourceList.add(resource);
		});

		iPage.setRecords(resourceList);
		return iPage;
	}

}
